﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BL;

namespace ShippingID.Controllers
{
    public class EntregaController : Controller
    {
        //
        // GET: /Entrega/
        private ServiceEntrega bl;
        private ServiceGeral blG;

        
        public  EntregaController()
        {
            bl = new ServiceEntrega();
            blG = new ServiceGeral();
        }
        public ActionResult Index()
        {
            CarregaCombos();
            /*
            ViewData["Caminhao"] = new BL.ServiceGeral().RetornarTodos<Modelo.Caminhao>();
            ViewData["Percurso"] = new BL.ServiceGeral().RetornarTodos<Modelo.Percurso>();

             */

            IList<Modelo.Entrega> entrega = blG.RetornarTodos<Modelo.Entrega>();
            foreach (Modelo.Entrega  item in entrega)
            {
                item.Percurso = blG.RetornarPorID<Modelo.Percurso>(item.IdPercurso);
                item.Caminhao= blG.RetornarPorID<Modelo.Caminhao>(item.IdCaminhao);

            }


            return View(entrega);
        }

        //
        // GET: /Entrega/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Entrega/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Entrega/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            Modelo.Entrega modelo = new Modelo.Entrega(Convert.ToInt32(collection[1]),"", Convert.ToDateTime(collection[0]), DateTime.Now, 0, Convert.ToInt32(collection[2]));
            if (ModelState.IsValid && bl.Gravar(modelo))
            {
                return RedirectToAction("Index");
            }

            return View(modelo);
        }
        
        //
        // GET: /Entrega/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Entrega/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Entrega/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Entrega/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public void CarregaCombos()
        {
            ViewData["Caminhao"] = new BL.ServiceGeral().RetornarTodos<Modelo.Caminhao>();
            ViewData["Percurso"] = new BL.ServiceGeral().RetornarTodos<Modelo.Percurso>();
            
        }


    }
}
